package logic

import (
	"backend/apps/book/models"
	"context"
	"github.com/pkg/errors"

	"backend/apps/book/proto"
	"backend/apps/book/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateTagLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateTagLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateTagLogic {
	return &UpdateTagLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateTagLogic) UpdateTag(in *proto.Tag) (*proto.CommonResponse, error) {
	_, err := l.svcCtx.TagModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if errors.Is(err, models.ErrNotFound) {
			return nil, errors.New("书籍标签不存在")
		}
		return nil, err
	}
	err = l.svcCtx.TagModel.Update(l.ctx, &models.Tag{
		Id:   in.Id,
		Name: in.Name,
	})
	return &proto.CommonResponse{}, err
}
